package com.ssh;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class FrequencySort_451 {
	@Test
	public void test() {
		System.out.println(frequencySort("assdadd"));
	}
	
	public String frequencySort(String s) {
		Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (char c : s.toCharArray()) {
        	map.put(c, map.getOrDefault(c, 0)+1);
        }
        int[][] c = new int[map.size()][2];
        int i = 0;
        for (Character x : map.keySet()) {
        	c[i][0] = x;
        	c[i][1] = map.get(x);
        	i++;
        }
        Arrays.sort(c,(y,z)->(z[1]-y[1]));
        StringBuffer sb = new StringBuffer();
        for (int j = 0, n = c.length; j < n; j++) {
        	for (int k = 0; k < c[j][1]; k++) {
        		sb.append((char)c[j][0]);
        	}
        }
        return sb.toString();
    }
}
